# encoding: UTF-8

require 'test/unit'
require 'lib/smppclient/util/byte_buffer'

class TestByteBuffer < MiniTest::Unit::TestCase

  include SmppClient::Util

  def test_initialize_empty
    bb = ByteBuffer.new
    assert_equal(0, bb.size)
    assert_equal([], bb.bytes)
    assert_equal("", bb.to_s)
  end

  def test_initialize_nonempty
    array = [1, 2, 3]
    bb = ByteBuffer.new(array)
    assert_equal(3, bb.size)
    assert_equal(array, bb.bytes)
    assert_equal("010203", bb.to_s)
  end

  def test_append_bytes
    array = [1, 2, 3]
    bb = ByteBuffer.new
    bb.append_bytes(array)
    assert_equal(3, bb.size)
    assert_equal(array, bb.bytes)
    assert_equal("010203", bb.to_s)
  end

  def test_append_byte
    array = [0, 255]
    bb = ByteBuffer.new
    bb.append_byte(array[0])
    bb.append_byte(array[1])
    assert_equal(2, bb.size)
    assert_equal(array, bb.bytes)
    assert_equal("00ff", bb.to_s)
  end

  def test_append_byte_fail
    bb = ByteBuffer.new
    bb.append_byte(-1)
    bb.append_byte(-255)
    bb.append_byte(256)
    assert_equal(0, bb.size)
  end

  def test_append_short
    bb = ByteBuffer.new
    bb.append_short(0)
    bb.append_short(256)
    bb.append_short(65535)
    assert_equal(6, bb.size)
    assert_equal([0, 0, 1, 0, 255, 255], bb.bytes)
    assert_equal("00000100ffff", bb.to_s)
  end

  def test_append_short_fails
    bb = ByteBuffer.new
    bb.append_short(-1)
    bb.append_short(-65535)
    bb.append_short(65536)
    assert_equal(0, bb.size)
  end

  def test_append_int
    bb = ByteBuffer.new
    bb.append_int(0)
    bb.append_int(256)
    bb.append_int(65536)
    bb.append_int(4294967295)
    assert_equal(16, bb.size)
    assert_equal([0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 255, 255, 255, 255], bb.bytes)
    assert_equal("000000000000010000010000ffffffff", bb.to_s)
  end

  def test_append_int_fails
    bb = ByteBuffer.new
    bb.append_int(-1)
    bb.append_int(-4294967295)
    bb.append_int(4294967296)
    assert_equal(0, bb.size)
  end

  def test_append_cstring
    bb = ByteBuffer.new
    bb.append_cstring('')
    bb.append_cstring('Hello')
    assert_equal(7, bb.size)
    assert_equal('0048656c6c6f00', bb.to_s)
  end

  def test_append_cstring_fails
    bb = ByteBuffer.new
    bb.append_cstring('Булат')
    assert_equal(0, bb.size)
  end

  def test_append_string
    bb = ByteBuffer.new
    bb.append_string('Привет', 'UTF-16BE')
    assert_equal(12, bb.size)
    assert_equal('041f04400438043204350442', bb.to_s)
  end

  def test_append_string_with_default_encoding
    bb = ByteBuffer.new
    bb.append_string('Hello')
    assert_equal(5, bb.size)
    assert_equal('48656c6c6f', bb.to_s)
  end

  def test_appenders_chain
    bb = ByteBuffer.new.append_byte(1).append_short(2).append_int(3).append_bytes([4, 5, 6])
    assert_equal('0100020000000304050648656c006c6f', bb.append_cstring('Hel').append_string('lo').to_s)
  end

  def test_read_byte
    bb = ByteBuffer.new
    bb.append_byte(0)
    assert_equal(0, bb.read_byte)
    bb = ByteBuffer.new
    bb.append_byte(255)
    assert_equal(255, bb.read_byte)
  end

  def test_read_short
    bb = ByteBuffer.new
    bb.append_short(0)
    assert_equal(0, bb.read_short)
    bb = ByteBuffer.new
    bb.append_short(256)
    assert_equal(256, bb.read_short)
    bb = ByteBuffer.new
    bb.append_short(65535)
    assert_equal(65535, bb.read_short)
  end

  def test_read_int
    bb = ByteBuffer.new
    bb.append_int(0)
    assert_equal(0, bb.read_int)
    bb = ByteBuffer.new
    bb.append_int(256)
    assert_equal(256, bb.read_int)
    bb = ByteBuffer.new
    bb.append_int(65536)
    assert_equal(65536, bb.read_int)
    bb = ByteBuffer.new
    bb.append_int(4294967295)
    assert_equal(4294967295, bb.read_int)
  end

  def test_remove_byte
    bb = ByteBuffer.new
    bb.append_byte(0)
    bb.append_byte(255)
    assert_equal(0, bb.remove_byte)
    assert_equal(255, bb.remove_byte)
    assert_equal(0, bb.size)
  end

  def test_remove_short
    bb = ByteBuffer.new
    bb.append_short(0)
    bb.append_short(256)
    bb.append_short(65535)
    assert_equal(0, bb.remove_short)
    assert_equal(256, bb.remove_short)
    assert_equal(65535, bb.remove_short)
    assert_equal(0, bb.size)
  end

  def test_remove_int
    bb = ByteBuffer.new
    bb.append_int(0)
    bb.append_int(256)
    bb.append_int(65536)
    bb.append_int(4294967295)
    assert_equal(0, bb.remove_int)
    assert_equal(256, bb.remove_int)
    assert_equal(65536, bb.remove_int)
    assert_equal(4294967295, bb.remove_int)
    assert_equal(0, bb.size)
  end

  def test_remove_cstring
    string1 = ''
    string2 = 'Hello'
    bb = ByteBuffer.new
    bb.append_cstring(string1)
    bb.append_cstring(string2)
    assert_equal(string1, bb.remove_cstring)
    assert_equal(string2, bb.remove_cstring)
    assert_equal(0, bb.size)
  end

  def test_remove_cstring_fail
    bb = ByteBuffer.new
    bb.append_string('Hello')
    assert_raises(SmppClient::SmppClientError) { bb.remove_cstring }
  end

  def test_remove_string
    string = 'Привет'
    encoding = 'UTF-16BE'
    bb = ByteBuffer.new
    bb.append_string(string, encoding)
    assert_equal('041f04400438043204350442', bb.to_s)
    assert_equal(string, bb.remove_string(12, encoding))
    assert_equal(0, bb.size)
  end

  def test_remove_bytes
    bb = ByteBuffer.new([1, 2, 3])
    assert_equal([1, 2], bb.remove_bytes(2).bytes)
    assert_equal([3], bb.bytes)
  end

end